package library.SocketData
{
	import flash.utils.ByteArray;
	import flash.utils.Endian;
	
	public class SocketData extends ByteArray
	{
		public function SocketData( p_bytes:ByteArray = null)
		{
			super();
			this.endian = Endian.LITTLE_ENDIAN; 
			if(p_bytes != null)
			{
				p_bytes.endian = Endian.LITTLE_ENDIAN;
				this.writeBytes(p_bytes,0,p_bytes.length);
			}
			this.position = 0;
		}
		
		public function writeInt32(value:int):void
		{
			this.writeInt(value);
		}
		
		public function writeInt16(value:int):void
		{
			this.writeShort(value);
		}
		
		public function writeInt8(value:int):void
		{
			this.writeByte(value);
		}
		public function writeRepeat(value:int):void
		{
			this.writeShort(value);
		}
		
		public function writeUInt32(value:int):void
		{
			this.writeUnsignedInt(value);
		}
		
		override public function writeFloat(value:Number):void
		{
			this.writeInt(int(value*1000));
		}
		
		public function writeString(value:String):void
		{
			var ba:ByteArray = new ByteArray;
			ba.endian = Endian.LITTLE_ENDIAN ;
			ba.writeMultiByte(value,"gb2312");
			ba.writeByte(0);
			this.writeShort(ba.length);
			this.writeBytes(ba,0,ba.length);
		}
		
		public function readInt32():int
		{
			return this.readInt();
		}
		
		public function readInt16():int
		{
			return this.readShort();
		}
		
		public function readInt8():int
		{
			return this.readByte();
		}
		
		public function readUInt8():int
		{
			return this.readUnsignedByte();
		}
		
		public function readUInt16():int
		{
			return this.readUnsignedShort();
		}
		
		public function readUInt32():int
		{
			return this.readUnsignedInt();
		}
		
		
		override public function readFloat():Number
		{
			return (this.readInt()/1000);
		}
		
		public function readString():String
		{
			var len:int = this.readShort();
			return this.readMultiByte(len,"gb2312");
		}
		
	}
}